package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.jboss.netty.util.internal.ReusableIterator;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Lz
 * @date 2020-03-06 20:49
 * @week 星期五
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //查询所有父类菜单名及id
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findAllParentMenu")
    public Result findAllParentMenu() {
        try {
            List<Map<String, Object>> allParentMenu = menuService.findAllParentMenu();
            return new Result(true, "查询所有父类菜单数据成功", allParentMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有父类菜单数据失败");
        }
    }

    //添加菜单
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            Result result = menuService.add(menu);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加菜单失败");
        }
    }

    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            Result result = menuService.edit(menu);
            return result;
        }catch (Exception e) {
            return new Result(false, "编辑菜单失败");
        }
    }

    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            Result result = menuService.delete(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败");
        }
    }

    @RequestMapping("/getMenus")
    public Result getMenus(String username) {
        try {
            //用户名->角色->菜单
            List<Menu> menus = menuService.getMenus(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
}
